﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhSystemBLL.basicData;
using dhDataAcess.dataAcess;
using dhDataAcess.Model;
using baseSet.importExport;
using dhsystem.Main;
using baseSet.power;

namespace dhsystem.basicForm
{
    public partial class FrmUserList : Form
    {
        userListBLL user = new userListBLL();
        userListModel userRow = new userListModel();
        workerListBLL workerList = new workerListBLL();
        accessGroupBLL accessGroup = new accessGroupBLL();
        public FrmUserList()
        {
            InitializeComponent();
        }
        //新增按钮，显示新增数据组件
        private void toolAdd_Click(object sender, EventArgs e)
        {
            bindingSourceworkerlist.DataSource = workerList.getworkerListAll();
            comboBoxWorkerNum.DataSource = bindingSourceworkerlist;
            comboBoxWorkerNum.DisplayMember = "name";
            comboBoxWorkerNum.ValueMember = "workernum";

            bindingSourceaccessgroup.DataSource = accessGroup.getaccessGroupAll();
            comboBoxGroupNum.DataSource = bindingSourceaccessgroup;
            comboBoxGroupNum.DisplayMember = "groupName";
            comboBoxGroupNum.ValueMember = "groupNum";
            checkAdd.Visible = true;
            if (lblState.Text == "编辑")
            {
                MessageBox.Show("当前在编辑数据状态，请先保存编辑数据或者取消再进行编辑");
                return;
            }
            lblState.Text = "新增";
            if (groupAdd.Visible == false)
            {
                groupAdd.Visible = true;
                groupList.Width = groupList.Width - 288;
                groupAdd.Location = new Point(groupList.Width + 20, groupList.Top);
                textUserNum.Focus();


            }
        }
        //打开窗口时显示列表数据并且隐藏新增数据组件
        private void FrmUserList_Load(object sender, EventArgs e)
        {
            bindingSourceUserList.DataSource = user.getuserListAll();
            dGridUserList.DataSource = bindingSourceUserList;
            groupList.Width = groupList.Width + groupAdd.Width;
            groupAdd.Visible = false;
            DcolumnSet.colSet(moduleInfo.userList , dGridUserList );
            toolStripStatusLabel1.Text = "数据总条数:" + dGridUserList.RowCount.ToString() + "条";

            sysDataSet.modulAaccessDataTable modulAaccessI = powerSet.modulPower(moduleInfo.userList );
            foreach (sysDataSet.modulAaccessRow modulAaccessRow in modulAaccessI)
            {
                buttonQuery.Enabled = modulAaccessRow.showAc;
                toolAdd.Enabled = modulAaccessRow.addAc;
                toolEdit.Enabled = modulAaccessRow.editAc;
                toolDel.Enabled = modulAaccessRow.delAc;
                toolPrint.Enabled = modulAaccessRow.printAc;
                toolExport.Enabled = modulAaccessRow.exportAc;
                //buttImport.Enabled = modulAaccessRow.importAc;

            }
        }
        //取消按钮，隐藏新增数据组件
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            groupList.Width = 288 + groupList.Width;
            groupAdd.Visible = false;
            lblState.Text = "正常";
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (lblState.Text == "新增")
            {
                if (textUserNum.Text == "")
                {
                    MessageBox.Show("用户编号不能为空");
                }
                else if (comboBoxWorkerNum.Text == "")
                {
                    MessageBox.Show("工号不能为空");

                }
                else if (comboBoxGroupNum.Text == "")
                {
                    MessageBox.Show("权限组不能为空");

                } 
                else
                {
                    try
                    {
                        DialogResult result = MessageBox.Show("是否新增用户！", "提示信息", MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {
                            userRow.userNum = textUserNum.Text;
                            userRow.workerNum = comboBoxWorkerNum.SelectedValue.ToString();
                            userRow.groupNum = comboBoxGroupNum.SelectedValue.ToString();
                            userRow.pwd = textUserpwd.Text;
                            userRow.createDate = DateTime.Now;
                            userRow.createUser = textcreateuser.Text;
                            userRow.updateDate = DateTime.Now;
                            userRow.updateUser = textupdateuser.Text;
                            userRow.mark = textmark.Text;
                            user.userListInsert(userRow);
                            bindingSourceUserList.DataSource = user.getuserListAll();
                            if (checkAdd.Checked == true)
                            {
                                lblState.Text = "新增";
                            }
                            else
                            {
                                lblState.Text = "正常";
                            }
                            return;
                        }
                    }
                    catch (FormatException ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
            }
            if (lblState.Text == "编辑")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否更改用户资料！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        userRow.userNum = textUserNum.Text;
                        userRow.workerNum = comboBoxWorkerNum.SelectedValue.ToString(); ;
                        userRow.groupNum = comboBoxGroupNum.SelectedValue.ToString();
                        userRow.pwd = textUserpwd.Text;
                        userRow.createDate = DateTime.Now;
                        userRow.createUser = textcreateuser.Text;
                        userRow.updateDate = DateTime.Now;
                        userRow.updateUser = textupdateuser.Text;
                        userRow.mark = textmark.Text;
                        user.userListUpdate(userRow);
                        bindingSourceUserList.DataSource = user.getuserListAll();
                        lblState.Text = "正常";
                        return;
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //工具栏的编辑按钮,修改数据
        private void toolEdit_Click(object sender, EventArgs e)
        {
            bindingSourceworkerlist.DataSource = workerList.getworkerListAll();
            comboBoxWorkerNum.DataSource = bindingSourceworkerlist;
            comboBoxWorkerNum.DisplayMember = "name";
            comboBoxWorkerNum.ValueMember = "workernum";

            bindingSourceaccessgroup.DataSource = accessGroup.getaccessGroupAll();
            comboBoxGroupNum.DataSource = bindingSourceaccessgroup;
            comboBoxGroupNum.DisplayMember = "groupName";
            comboBoxGroupNum.ValueMember = "groupNum";
            comboBoxWorkerNum.SelectedValue = dGridUserList.Rows[dGridUserList.CurrentRow.Index].Cells[2].Value.ToString();
            comboBoxGroupNum.SelectedValue = dGridUserList.Rows[dGridUserList.CurrentRow.Index].Cells[3].Value.ToString();
            checkAdd.Visible = false;
            try
            {
                if (lblState.Text == "新增")
                {
                    MessageBox.Show("当前在新增数据状态，请先保存新增数据或者取消再进行编辑");
                    return;
                }

                lblState.Text = "编辑";

                if (groupAdd.Visible == false)
                {
                    groupAdd.Visible = true;
                    groupList.Width = groupList.Width - 288;
                    groupAdd.Location = new Point(groupList.Width + 20, groupList.Top);
                }

                DataGridViewRow currentRow = dGridUserList.CurrentRow;
                textUserNum.Text = currentRow.Cells["userNum"].Value.ToString();
                comboBoxWorkerNum.Text = currentRow.Cells["workerNum"].Value.ToString();
                comboBoxGroupNum.Text = currentRow.Cells["groupNum"].Value.ToString();
                textUserpwd.Text = currentRow.Cells["pwd"].Value.ToString();
                textcreateuser.Text = currentRow.Cells["createUser"].Value.ToString();
                textupdateuser.Text = currentRow.Cells["updateUser"].Value.ToString();
                textmark.Text = currentRow.Cells["mark"].Value.ToString();
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //删除按钮，删除选中行数据
        private void toolDel_Click(object sender, EventArgs e)
        {
            string userNum = dGridUserList.Rows[dGridUserList.CurrentRow.Index].Cells[1].Value.ToString();
            try
            {
                DialogResult result = MessageBox.Show("是否删除权限组编号为" + userNum + "的权限组信息！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {

                    bindingSourceUserList.DataSource = user.userListDel(userNum);
                    dGridUserList.DataSource = bindingSourceUserList;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            bindingSourceUserList.DataSource = user.getuserListAll();
        }
        //工具栏查询按钮，查询所有列表数据
        private void toolQuery_Click(object sender, EventArgs e)
        {
            FrmDataQuery query = new FrmDataQuery(moduleInfo.userList );
            DialogResult result = query.ShowDialog();
            if (result == DialogResult.Yes)
            {
                dGridUserList.DataSource = query.rTable;
            }
        }
        //查询按钮，按条件查询数据
        private void buttonQuery_Click(object sender, EventArgs e)
        {
            try
            {
                bindingSourceaccessgroup .DataSource =user.getuserListAll ("%" + textUserNumQuery .Text  + "%");
                dGridUserList.DataSource = bindingSourceaccessgroup;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //工具栏退出按钮
        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //工号控件的单机事件
        private void comboBoxWorkerNum_Click(object sender, EventArgs e)
        {
            bindingSourceworkerlist.DataSource = workerList.getworkerListAll();
            comboBoxWorkerNum.DataSource = bindingSourceworkerlist;
            comboBoxWorkerNum.DisplayMember = "name";
            comboBoxWorkerNum.ValueMember = "workernum";

        }

        private void comboBoxGroupNum_Click(object sender, EventArgs e)
        {
            bindingSourceaccessgroup.DataSource = accessGroup.getaccessGroupAll();
            comboBoxGroupNum.DataSource = bindingSourceaccessgroup;
            comboBoxGroupNum.DisplayMember = "groupName";
            comboBoxGroupNum.ValueMember = "groupNum";
        }

        private void FrmUserList_Resize(object sender, EventArgs e)
        {
            if (groupAdd.Visible == true)
            {
                groupAdd.Location = new Point(groupList.Width + 20, groupList.Top);
            }
        }
        //datagridview第一列显示行号
        private void dGridUserList_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }

        private void toolExport_Click(object sender, EventArgs e)
        {
            FrmExcelExport frm = new FrmExcelExport(moduleInfo.userList , dGridUserList , "操作员信息");
            frm.ShowDialog();
        }

   
    }
}
